/* (c)2010-2011 Andrea Caltroni (andrea.caltroni@pd.infn.it), INFN (www.infn.it)
 *
 * This file is part of the IGI Survey Portlet (from here on shortened to IGISP).
 * IGISP is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) any 
 * later version.
 *
 * IGISP is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with IGISP. If not, see <http://www.gnu.org/licenses/>.
 */
package it.infn.grid.liferay.survey.modules.admin.web;

import java.beans.PropertyEditorSupport;

import org.ajm.spring.survey.modules.admin.model.Survey;
import org.ajm.spring.survey.modules.admin.service.SurveyService;

/**
 * Property editor for {@link org.ajm.spring.survey.modules.admin.model.Survey Survey} 
 * type properties.
 * 
 * @author Andrea Caltroni, andrea.caltroni@pd.infn.it, INFN
 */
public class SurveyPropertyEditor extends PropertyEditorSupport {
    
    private SurveyService surveyService;
    
    // ------------------------------------------------------------------------|

    /**
     * The default constructor.
     * 
     * @param surveyService
     */
    public SurveyPropertyEditor(SurveyService surveyService) {
        
        super();
        this.surveyService = surveyService;
    }

    // ------------------------------------------------------------------------|

    /**
     * Sets the property value by parsing a given String.
     */
    @Override
    public void setAsText(String surveyId) {
        
        Survey surv = getSurveyService().findById(Long.parseLong(surveyId));
        setValue(surv);
    }

    // ------------------------------------------------------------------------|

    /**
     * Gets the property value as text.
     */
    @Override
    public String getAsText() {
        
        if(getValue() == null) {
            return null;
        }
        
        Survey surv = (Survey)getValue();
        return surv.getId().toString();
    }

    // ------------------------------------------------------------------------|

    /**
     * @return the surveyService
     */
    public SurveyService getSurveyService() {
        
        return surveyService;
    }

    // ------------------------------------------------------------------------|

    /**
     * @param surveyService the surveyService to set
     */
    public void setSurveyService(SurveyService surveyService) {
        
        this.surveyService = surveyService;
    }
    
    // ------------------------------------------------------------------------|

}
